package com.kuhyygy.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.kuhyygy.R;
import com.kuhyygy.adapter.DbUserAdapter;
import com.kuhyygy.bean.User;
import com.kuhyygy.sqlite.dao.DBHelper;

public class DbActivity extends Activity {
	
	private static final String TAG = DbActivity.class.getName();
	
	private Button dbCreate;
	private Button dbAdd;
	private Button dbUpdate;
	private Button dbDel;
	private Button dbQry;
	
	private ListView lv;
	private DbUserAdapter userAdapter;
	
	private DBHelper dbHelper;
	private SQLiteDatabase db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_db);
		dbCreate = (Button) findViewById(R.id.createDB);
		dbAdd = (Button) findViewById(R.id.dbAdd);
		dbQry = (Button) findViewById(R.id.dbQuery);
		dbHelper = new DBHelper(this);
		dbCreate.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				dbHelper.getWritableDatabase();
			}
		});
		loadData();
		dbAdd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				showCustomDialog();
			}
		});
		dbQry.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				userAdapter.notifyDataSetChanged();
				alert("刷新成功");
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.db, menu);
		return true;
	}
	
	private void loadData() {
		db = dbHelper.getWritableDatabase();
		Cursor cursor = db.query("t_person", null, null, null, null, null, "_id desc");
		List<User> users = new ArrayList<User>();
		if (cursor.moveToFirst()) {
			do {
				Integer id = cursor.getInt((cursor.getColumnIndex("_id")));
				String name = cursor.getString(cursor.getColumnIndex("name"));
				String gender = cursor.getString(cursor.getColumnIndex("gender"));
				User u = new User();
				u.setId(id);
				u.setName(name);u.setGender(gender);
				users.add(u);
			} while (cursor.moveToNext());
		}
		userAdapter = new DbUserAdapter(this, R.layout.dbuseritem, users);
		lv = (ListView) this.findViewById(R.id.dbListView);
		lv.setAdapter(userAdapter);
	}
	
	protected void alert(String text) {
		Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
	}
	
	protected void showCustomDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		final LinearLayout ll = (LinearLayout) getLayoutInflater().inflate(R.layout.dialog_dbuser, null);
		builder.setView(ll)	// 一定要在setView之前,把view准备好,不然下面的getText不会有数据
				.setPositiveButton(R.string.OK,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int id) {
								EditText etUser = (EditText) ll.findViewById(R.id.dbUserName);
								EditText etGen = (EditText) ll.findViewById(R.id.dbUserGender);
								User u = new User();
								u.setName(etUser.getText().toString());
								u.setGender(etGen.getText().toString());
								ContentValues values = new ContentValues();
								values.put("name", u.getName());
								values.put("gender", u.getGender());
								db.insert("t_person", null, values);
								loadData();
							}
						})
				.setNegativeButton(R.string.CANCEL,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.dismiss();
							}
						});
		builder.create().show();
	}

}
